/**
 * 接口返回文本信息转换
 */
export const handleText = oldText => {
	let newText = "<p>" + oldText.replace(/\n/g, '<br/>').replace(/\s/g, '&emsp;') + "</p>"
	newText = newText.replace(/&emsp;<br\/>/g, '<br/>');
	return newText
}
/**
 * 验证是否为空
 */
export const hasContent = content => {
	return content.replace(/\ +/g, "").replace(/[\r\n]/g, "") != ''
}
/**
 * 邮箱校验
 */
export const checkMailbox = mailbox => {
	return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(mailbox)
};
/**
 * 手机号校验
 */
export const checkPhone = phone => {
	return /^[1][0-9]{10}$/.test(phone)
};
/**
 * 身份证校验
 */
export const checkIdcard = idCard => {
	if(idCard.slice(17, 18) == "x") {
		idCard = idCard.replace("x", "X");
	}
	idCard = idCard.toString();
	var city = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江 ",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北 ",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏 ",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门",
		91: "国外 "
	};
	var tip = "";
	var pass = true;

	if(!idCard ||
		!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(
			idCard
		)
	) {
		tip = "身份证号格式错误";
		pass = false;
	} else if(!city[idCard.substr(0, 2)]) {
		tip = "地址编码错误";
		pass = false;
	} else {
		//18位身份证需要验证最后一位校验位
		if(idCard.length == 18) {
			idCard = idCard.split("");
			//∑(ai×Wi)(mod 11)
			//加权因子
			var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
			//校验位
			var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
			var sum = 0;
			var ai = 0;
			var wi = 0;
			for(var i = 0; i < 17; i++) {
				ai = idCard[i];
				wi = factor[i];
				sum += ai * wi;
			}
			var last = parity[sum % 11];
			if(parity[sum % 11] != idCard[17]) {
				tip = "校验位错误";
				pass = false;
			}
		}
	}
	var obj = {
		status: pass,
		msg: tip
	};
	if(!pass) {
		return obj;
	}
	return obj;
};

/**
 * @检查证件合法性
 */
export const checkIdcardValid = (cardtype_id, cardnum) => {
	let result = {};
	result.status = true
	result.errtips = ''
	//2护照 '/^[a-z0-9]{5,17}$/i'
	//3港澳通行证、4台湾通行证、5军官证、6士兵证 '/^[a-z0-9]{5,12}$/i'
	switch(cardtype_id) {
		case 1:
			if(!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(cardnum)) {
				result.status = false
				result.errtips = '身份证格式错误'
			}
			break;
		case 2:
			if(!/^[a-z0-9]{5,17}$/i.test(cardnum)) {
				result.status = false
				result.errtips = '护照格式错误'
			}
			break;
		case 3:
		case 4:
		case 5:
		case 6:
			if(!/^[a-z0-9]{5,12}$/i.test(cardnum)) {
				result.status = false
				result.errtips = '证件格式错误'
			}
			break;
	}
	return result;
}

/**
 * 根据身份证号计算年龄
 */
export const calcIDCard = (IDCard) => {
	//获取用户身份证号码
	var userCard = IDCard.toString();
	//获取出生年月日
	var yearBirth = parseInt(userCard.substring(6, 10)); //出生年份
	var monthBirth = parseInt(userCard.substring(10, 12)); //出生月份
	var dayBirth = parseInt(userCard.substring(12, 14)); //出生日
	//获取当前年月日并计算年龄
	var myDate = new Date();
	var yearNow = parseInt(myDate.getFullYear()); //当前年份
	var monthNow = parseInt(myDate.getMonth() + 1); //当前月份
	var dayNow = parseInt(myDate.getDate()); //当前日
	var age = yearNow - yearBirth;
	if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
		age--;
	}
	return age;
}
/**
 * 解决遮罩层滚动穿透问题，分别在遮罩层弹出后和关闭前调用
 */
export const ModalHelper = (() => {
	var scrollTop;
	return {
		afterOpen: function() {
			scrollTop = document.scrollingElement.scrollTop;
			document.body.style.position = 'fixed';
			document.body.style.width = '100%';
			document.body.style.overflow = 'hidden';
			document.body.style.top = -scrollTop + 'px';
		},
		beforeClose: function() {
			// scrollTop lost after set position:fixed, restore it back.
			document.body.style.position = '';
			document.body.style.width = '';
			document.body.style.overflow = '';
			document.body.style.top = '';
			document.scrollingElement.scrollTop = scrollTop;
		}
	};
})()

/**
 * 验证是否是微信环境
 */
export const checkWx = () => {
	let browser = {
		versions: (function() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			return {
				//移动终端浏览器版本信息
				trident: u.indexOf("Trident") > -1, //IE内核
				presto: u.indexOf("Presto") > -1, //opera内核
				webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
				gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核
				mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
				ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
				android: u.indexOf("Android") > -1 || u.indexOf("Linux") > -1, //android终端或uc浏览器
				iPhone: u.indexOf("iPhone") > -1, //是否为iPhone或者QQHD浏览器
				iPad: u.indexOf("iPad") > -1, //是否iPad
				webApp: u.indexOf("Safari") == -1 //是否web应该程序，没有头部与底部
			};
		})(),
		language: (navigator.browserLanguage || navigator.language).toLowerCase()
	};
	if(browser.versions.mobile) {
		var ua = navigator.userAgent.toLowerCase();
		//在微信中打开
		if(ua.match(/MicroMessenger/i) == "micromessenger") {
			return true;
		} else {
			return false;
		}
	}
};
/**
 * 秒数转换成(--:--,如03:25)时间格式
 */
export const transformTime = secondVal => {
	let minute = Math.floor(secondVal / 60);
	if(minute < 10) {
		minute = '0' + minute
	}
	let second = Math.ceil(secondVal % 60);
	if(second < 10) {
		second = '0' + second
	}
	return minute + ':' + second
};
export const getCookie = cname => {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i].trim();
		if(c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return "";
}

//转换年月日方法
export const getDate = (format, str) => {  
    var oDate = new Date(str),  
    oYear = oDate.getFullYear(),  
    oMonth = oDate.getMonth()+1,  
    oDay = oDate.getDate(),  
    oHour = oDate.getHours(),  
    oMin = oDate.getMinutes(),  
	oSec = oDate.getSeconds(),
	oTime
    if(format == 'yyyy-mm-dd'){
   	    oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSec);//最后拼接时间
    }else if(format == 'yyyy/mm/dd'){
    	oTime = oYear +'/'+ getzf(oMonth) +'/'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSec);//最后拼接时间
    }
    return oTime;  
 };
 //补0操作  
 function getzf(num){
	if(parseInt(num) < 10){  
		num = '0'+num;  
	}  
	return num; 
} 